package lottery;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.WindowConstants;

import dao.DaoFactory;
import dao.User;
import dao.UserDao;

public class UserFrame extends JFrame{
	private User user1;

	 public  UserFrame(User user){
		        user1=user;
		         init();
		         super.setTitle("彩票购买抽奖程序");
		         int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		 		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		 		super.setLocation((width - 400) / 2, (height - 350) / 2);
		 		this.setVisible(true);
		 		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		     }
//	 public UserFrame(User user) {
		 
	// }

		     
		     public void init() {

		     	JLabel label1 = new JLabel();
		     	JLabel label2 = new JLabel();
		         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		        
			        getContentPane().setBackground(Color.CYAN);
		        label1.setFont(new java.awt.Font("宋体", 1, 25)); // NOI18N
		         label1.setText("欢迎来到福利彩票36选7网");
		       
		         JButton bt1=new JButton();
		         JButton bt2=new JButton();
		         JButton bt3=new JButton();
		         JButton bt4=new JButton();
		        
		       bt1.setText("充值");
		       bt1.addActionListener(new ActionListener() {
		           

		 			@Override
		 			public void actionPerformed(ActionEvent e) {
		 				new Pay(user1);
		 				
		 			}
		         });
		       bt2.setText("查询");
		       bt2.addActionListener(new ActionListener() {
		    	   @Override
		 			public void actionPerformed(ActionEvent e) {
		 				// TODO Auto-generated method stub
		    		 new Show(user1);
		 				
		 			}
		         });
		       bt3.setText("购买彩票");
		       bt3.addActionListener(new ActionListener() {
		    	   @Override
		 			public void actionPerformed(ActionEvent e) {
		 			new  BuyTickets(user1);
		 				
		 			}
		         });
		       bt4.setText("退出登录");
		       bt4.addActionListener(new ActionListener() {
		    	   @Override
		 			public void actionPerformed(ActionEvent e) {
		 				// TODO Auto-generated method stub
		    		   setVisible(false);
		 				
		 			}
		         });  
		       GroupLayout layout = new GroupLayout(getContentPane());
		       getContentPane().setLayout(layout);
		       layout.setHorizontalGroup(
		           layout.createParallelGroup(GroupLayout.Alignment.LEADING)
		           .addGroup(layout.createSequentialGroup()
		               .addContainerGap(40, Short.MAX_VALUE)
		               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
		                   .addGroup(GroupLayout.Alignment.CENTER, layout.createSequentialGroup()
		                       .addComponent(label1)
		                       )
		                   
		                   .addGroup(GroupLayout.Alignment.LEADING,layout.createSequentialGroup()
		                       .addGap(10, 10, 10)
		                       .addComponent(bt1))
		                   .addGroup(GroupLayout.Alignment.LEADING,layout.createSequentialGroup()
		                           .addGap(100, 100, 100)
		                           .addComponent(bt2))
		                   .addGroup(GroupLayout.Alignment.LEADING,layout.createSequentialGroup()
		                           .addGap(200, 200, 200)
		                           .addComponent(bt3))
		                   .addGroup(GroupLayout.Alignment.LEADING,layout.createSequentialGroup()
		                           .addGap(310, 310, 310)
		                           .addComponent(bt4)
		                           .addContainerGap(50, Short.MAX_VALUE))
		                       ))
		       );
		       
		       layout.setVerticalGroup(
		               layout.createParallelGroup(GroupLayout.Alignment.LEADING)
		               .addGroup(layout.createSequentialGroup()
		             		  
		                   .addContainerGap()
		                   .addComponent(label1)
		                   .addGap(10, 10, 10)
		                   .addGap(60)
		                   .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		                 		  
		                       .addComponent(bt1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
		                   
		                   .addComponent(bt2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
		                   
		                   .addComponent(bt3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
		                   
		                   .addComponent(bt4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
		                   .addGap(60)
		                   ))
		           );

		           pack();
		           
		           
		           
		        
		         

		        
		     }


}

 class Pay extends JFrame {
	
	
	 User user2=new User();
	
	
	 public Pay(User user) {
		  user2=user;
	        init();
	        super.setTitle("彩票购买抽奖程序");
	        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
			int height = Toolkit.getDefaultToolkit().getScreenSize().height;
			super.setLocation((width - 400) / 2, (height - 350) / 2);
			//this.setLocation(200, 250);
			this.setSize(300, 150);
			this.setVisible(true);
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    }
	 public void init() {
		 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	        getContentPane().setBackground(Color.CYAN);
		 JLabel label = new JLabel();
		 
		 JButton bt1 =new JButton();
		 JButton bt2 =new JButton();
		
		JTextField paytext= new JTextField();
		label.setText("金额：");
		bt1.setText("支付");
		 bt1.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) {
	            	UserDao userDao = DaoFactory.getUserDaoInstance();
	            	User u=new User();
	            	try {
						u=userDao.QuerryByUsername(user2.getUsername());
					} catch (Exception e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
	            	
	            	
	            	double money=Double.parseDouble(paytext.getText())+u.getMoney();
	            	user2.setMoney(money);
	            	
	            	try {
						userDao.Update(user2);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
	            	
	            	
	            	JOptionPane.showMessageDialog(null, "充值成功！");
	                
	            }
	        });
		bt2.setText("返回");
		 bt2.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) {  	
	            	setVisible(false);
	            }
	        });
		 
		 
		 
		  // 为指定的 Container 创建 GroupLayout  
	        GroupLayout layout = new GroupLayout(this.getContentPane());  
	        this.getContentPane().setLayout(layout);  
	        //创建GroupLayout的水平连续组，，越先加入的ParallelGroup，优先级级别越高。  
	        
	        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();  
	       hGroup.addGap(30);//添加间隔  
	        hGroup.addGroup(layout.createParallelGroup().addComponent(label)  
	                .addComponent(bt1));  
	        hGroup.addGap(15);  
	        hGroup.addGroup(layout.createParallelGroup().addComponent(paytext)
	        		.addComponent(bt2));  
	        hGroup.addGap(30);  
	        layout.setHorizontalGroup(hGroup);  
	        //创建GroupLayout的垂直连续组，，越先加入的ParallelGroup，优先级级别越高。  
	        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();  
	        vGroup.addGap(30);  
	        vGroup.addGroup(layout.createParallelGroup().addComponent(label)
	        		.addComponent(paytext));  
	        vGroup.addGap(20);  
	        vGroup.addGroup(layout.createParallelGroup().addComponent(bt1)  
	                .addComponent(bt2));  
	       
	       
	        vGroup.addGap(30);  
	        //设置垂直组  
	        layout.setVerticalGroup(vGroup);  
	        //pack();
	 }
	

}
 
